﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;

using VirtualDigibox.Common;

namespace VirtualDigibox.BusinessService
{
    public static class SettingsManager
    {

        #region Properties

        private static string _VLCEXE = "";
        public static string VLCEXE
        {
            get
            {
                if (string.IsNullOrEmpty(_VLCEXE))
                {
                    _VLCEXE = GetRegValue(Constants.RegistryKeys.VLCEXE);
                }
                return _VLCEXE;
            }
            set
            {
                SetRegValue(Constants.RegistryKeys.VLCEXE, value);
                _VLCEXE = value;
            }
        }

        private static string _VideoSavePath = "";
        public static string VideoSavePath
        {
            get
            {
                if (string.IsNullOrEmpty(_VideoSavePath))
                {
                    _VideoSavePath = GetRegValue(Constants.RegistryKeys.VideoSavePath);
                }
                return _VideoSavePath;
            }
            set
            {
                SetRegValue(Constants.RegistryKeys.VideoSavePath, value);
                _VideoSavePath = value;
            }
        }
        private static string _VLCArgs = "";
        public static string VLCArgs
        {
            get
            {
                if (string.IsNullOrEmpty(_VLCArgs))
                {
                    _VLCArgs = GetRegValue(Constants.RegistryKeys.VLCArgs);
                }
                return _VLCArgs;
            }
            set
            {
                SetRegValue(Constants.RegistryKeys.VLCArgs, value);
                _VLCArgs = value;
            }
        }
        private static string _FileNameTemplate = "";
        public static string FileNameTemplate
        {
            get
            {
                if (string.IsNullOrEmpty(_FileNameTemplate))
                {
                    _FileNameTemplate = GetRegValue(Constants.RegistryKeys.FileNameTemplate);
                }
                return _FileNameTemplate;
            }
            set
            {
                SetRegValue(Constants.RegistryKeys.FileNameTemplate, value);
                _FileNameTemplate = value;
            }
        }
        #endregion Properties

        #region Methods
        /// <summary>
        /// uses Virtual Digibox Registry root as a keyName
        /// </summary>
        /// <param name="valueName">Virtual Digibox Registry root subKey</param>
        /// <returns>Value or String.Empty if valeuName is missing on empty</returns>
        private static string GetRegValue(string valueName)
        {
            return GetRegValue(Constants.RegistryKeys.VDBRoot, valueName);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="keyName">Registry keyName</param>
        /// <param name="valueName">KeyName subKey</param>
        /// <returns>Value or String.Empty if valeuName is missing on empty</returns>
        private static string GetRegValue(string keyName, string valueName)
        {
            object registryVal = Registry.GetValue(keyName, valueName, "");
            if (registryVal != null)
                return registryVal as string;
            else
                return string.Empty;
        }

        private static void SetRegValue(string valueName, string value)
        {
            SetRegValue(Constants.RegistryKeys.VDBRoot, valueName, value);
        }
        private static void SetRegValue(string keyName, string valueName, string value)
        {
            Registry.SetValue(keyName, valueName, value, RegistryValueKind.String);
        } 
        #endregion Methods
    }
}
